package cn.lopr.im.android.mvp.presenter;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.User;
import cn.lopr.im.android.common.Constant;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.common.app.Activity;
import cn.lopr.im.android.db.entity.Message;
import cn.lopr.im.android.db.helper.DaoUtilsStore;
import cn.lopr.im.android.db.service.MessageDaoService;
import cn.lopr.im.android.mvp.contact.IAddFriendContact;
import cn.lopr.im.android.mvp.model.AddFriendModel;
import cn.lopr.im.android.mvp.view.activities.MainActivity;
import cn.lopr.im.android.utils.DateUtil;
import cn.lopr.im.android.utils.GsonUtil;
import cn.lopr.im.android.utils.IMWebSocketClient;
import cn.lopr.im.android.utils.UiUtils;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class AddFriendPresenterImpl implements IAddFriendContact.IAddFriendPresenter {

    private static final String TAG = "AddFriendPresenterImpl";
    private final IAddFriendContact.IAddFriendModel addFriendModel;
    private final IAddFriendContact.IAddFriendView addFriendView;
    private final Activity activity;

    public AddFriendPresenterImpl(Activity activity, IAddFriendContact.IAddFriendView addFriendView) {
        this.addFriendModel = new AddFriendModel();
        this.activity = activity;
        this.addFriendView = addFriendView;
    }

    @Override
    public void searchFriend() {

        if (!UiUtils.isNetworkAvailable()) {
            addFriendView.showNetworkError();
            return;
        }
        addFriendView.showLoading(this.activity);

        addFriendModel.searchFriend(this.addFriendView.getToken(this.activity.getApplicationContext()),
                this.addFriendView.getSearchContent(), new OnCallBackListener<CallbackResult<List<User>>, String>() {
                    @Override
                    public void success(CallbackResult<List<User>> result) {
                        addFriendView.searchFriendSuccess(result.getData());
                        addFriendView.disLoading();
                    }

                    @Override
                    public void failure(String errMsg) {
                        addFriendView.showVerifyFailed(errMsg);
                        addFriendView.disLoading();
                    }
                });
    }

    @Override
    public void addFriend(String phone) {
        if (!UiUtils.isNetworkAvailable()) {
            addFriendView.showNetworkError();
            return;
        }

        addFriendModel.addFriend(this.addFriendView.getToken(this.activity.getApplicationContext()),
                phone, new OnCallBackListener<CallbackResult<List<User>>, String>() {
                    @Override
                    public void success(CallbackResult<List<User>> result) {
                        List<Message> messageByFriendApply = MessageDaoService.getMessage(phone,
                                MainActivity.user.getPhone(),
                                Constant.MessageType.SEND_ADD.getCode());
                        MessageDaoService.setMessageIsRead(messageByFriendApply);
                        addFriendView.addFriendSuccess(result.getMsg());

                    }

                    @Override
                    public void failure(String errMsg) {
                        addFriendView.showVerifyFailed(errMsg);
                    }
                });
    }

    @Override
    public void getAddFriendApply() {
        List<Message> messageByFriendApply = MessageDaoService.getMessageByType(MainActivity.user.getPhone(),
                Constant.MessageType.SEND_ADD.getCode() + "");
        List<String> phones = new ArrayList<>();
        for (Message message : messageByFriendApply) {
            phones.add(message.getFromUser());
        }
        addFriendModel.getFriendByPhones(this.addFriendView.getToken(this.activity.getApplicationContext()),
                phones, new OnCallBackListener<CallbackResult<List<User>>, String>() {
                    @Override
                    public void success(CallbackResult<List<User>> result) {
                        addFriendView.searchFriendSuccess(result.getData());
                    }

                    @Override
                    public void failure(String errMsg) {
                        addFriendView.showVerifyFailed(errMsg);
                    }
                });
    }

    @Override
    public void sendMessage(String toUserPhone) {
        String content = "add";

        Message message = new Message().setFromUser(MainActivity.user.getPhone())
                .setToUser(toUserPhone)
                .setType(Constant.MessageType.SEND_ADD.getCode())

                .setDate(DateUtil.getTime()).setGroupId(null)
                .setMsg(content);
        Log.i(TAG, "sendMessage: " + message);

        String strMessage = GsonUtil.objectToJson(message);

        IMWebSocketClient.getWebSocketClient().send(strMessage);
        DaoUtilsStore.getInstance().getMessageDaoUtils().insert(message.setIsRead(1));
    }

}
